package com.xingzhe.content.api;


import com.xingzhe.base.exception.ValidationGroups;
import com.xingzhe.base.model.PageParams;
import com.xingzhe.base.model.PageResult;
import com.xingzhe.content.model.dto.AddCourseDto;
import com.xingzhe.content.model.dto.CourseBaseInfoDto;
import com.xingzhe.content.model.dto.EditCourseDto;
import com.xingzhe.content.model.dto.QueryCourseParamsDto;
import com.xingzhe.content.model.po.CourseBase;
import com.xingzhe.content.service.Impl.CourseBaseInfoServiceImpl;

import com.xingzhe.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 课程信息管理接口
 */
@Api(value = "课程信息管理接口",tags = "课程信息管理接口")
@RestController
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoServiceImpl courseBaseInfoService;

    @ApiOperation(value = "课程信息分页查询的接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams,@RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        //当前登录的用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //用户所属的机构id
        Long companyId = null;
        assert user != null;
        if (StringUtils.isNotEmpty(user.getCompanyId())){
            companyId=Long.parseLong(user.getCompanyId());
        }
        PageResult<CourseBase> courseBasePageResult = courseBaseInfoService.queryCourseBaseList(companyId,pageParams, queryCourseParamsDto);
        return courseBasePageResult;

    }
    @ApiOperation(value = "新增课程的接口")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated AddCourseDto addCourseDto){
        //假设一个机构id
        Long companyId=1232141425L;
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }

    @ApiOperation("根据课程id查询的接口")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable(value = "courseId")Long courseId){
        //获取当前用户的身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        assert user != null;
        System.out.println(user.getUsername());
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }
    @ApiOperation("修改课程信息的接口")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        //假设一个机构id
        Long companyId=1232141425L;
        return courseBaseInfoService.updateCourseBase(companyId,editCourseDto);
    }

    // /course/87
    @ApiOperation("删除课程的接口")
    @DeleteMapping("/course/{courseId}")
    public CourseBase deleteCourse(@PathVariable(value = "courseId")Long courseId){
        return courseBaseInfoService.deleteCourse(courseId);
    }

}
